package com.suneast.suneaststore;

import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;

import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.suneast.suneaststore.app.AccountManager;
import com.suneast.suneaststore.app.Latte;
import com.suneast.suneaststore.bean.personal.trace.MyObjectBox;
import com.suneast.suneaststore.util.net.interceptors.TokenInterceptor;
import com.suneast.suneaststore.util.net.upload.UploadHandler;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.BuglyStrategy;
import com.tencent.bugly.beta.Beta;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import net.danlew.android.joda.JodaTimeAndroid;

import io.objectbox.BoxStore;

/**
 * Created by zestz on 2018/3/19.
 */

public final class App extends Application {

    private static BoxStore boxStore;

    private static App app;

    private static String appName = null;

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        appName = getAppName();
        Latte.init(this)
                .withLoaderDelayed(1000)
                .withApiHost(API.HOST)//服务器地址
                .withInterceptor(new TokenInterceptor())
                .withWeChatAppId("wxfc2894f6741ed293")//微信AppKey
                .withWeChatAppSecret("779cddbd97d184c52ddd8c463f1d6356")//微信AppSecret
                .configure();
        boxStore = MyObjectBox.builder().androidContext(App.this).build();
        initBugly();

        AccountManager.init();
        JodaTimeAndroid.init(this);

        ClassicsHeader.REFRESH_HEADER_PULLING = getString(R.string.refresh_header_pulling);
        ClassicsHeader.REFRESH_HEADER_REFRESHING = getString(R.string.refresh_header_refreshing);
        ClassicsHeader.REFRESH_HEADER_LOADING = getString(R.string.refresh_header_loading);
        ClassicsHeader.REFRESH_HEADER_RELEASE = getString(R.string.refresh_header_release);
        ClassicsHeader.REFRESH_HEADER_FINISH = getString(R.string.refresh_header_finish);
        ClassicsHeader.REFRESH_HEADER_FAILED = getString(R.string.refresh_header_failed);
        ClassicsHeader.REFRESH_HEADER_UPDATE = getString(R.string.refresh_header_update);

        ClassicsFooter.REFRESH_FOOTER_PULLING = getString(R.string.pull_up_load_more);
        ClassicsFooter.REFRESH_FOOTER_RELEASE = getString(R.string.footer_release);
        ClassicsFooter.REFRESH_FOOTER_REFRESHING = getString(R.string.footer_refreshing);
        ClassicsFooter.REFRESH_FOOTER_LOADING = getString(R.string.footer_loading);
        ClassicsFooter.REFRESH_FOOTER_FINISH = "";
        ClassicsFooter.REFRESH_FOOTER_FAILED = getString(R.string.footer_failed);
        ClassicsFooter.REFRESH_FOOTER_NOTHING = getString(R.string.no_more_data);

        final IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
        msgApi.registerApp(API.APP_ID);
        UploadHandler.requestToken();
    }

    public static BoxStore getBoxStore() {
        return boxStore;
    }

    public static App getInstance() {
        return app;
    }

    public static String getName() {
        return appName;
    }

    private String getAppName() {
        try {
            PackageManager packageManager = getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(
                    getPackageName(), 0);
            int labelRes = packageInfo.applicationInfo.labelRes;
            return getResources().getString(labelRes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static final String BUGLY_APP_ID = "923ccd43ab"; //  bugly上注册的appid
    public static final String BUGLY_APP_CHANNEL = "DEBUG"; // 自定义渠道

    private void initBugly() {
        /***** Beta高级设置 *****/
        /**
         * true表示app启动自动初始化升级模块; false不会自动初始化;
         * 开发者如果担心sdk初始化影响app启动速度，可以设置为false，
         * 在后面某个时刻手动调用Beta.init(getApplicationContext(),false);
         */
        Beta.autoInit = true;

        /**
         * true表示初始化时自动检查升级; false表示不会自动检查升级,需要手动调用Beta.checkUpgrade()方法;
         */
        Beta.autoCheckUpgrade = true;

        /**
         * 设置升级检查周期为60s(默认检查周期为0s)，60s内SDK不重复向后台请求策略);
         */
        Beta.upgradeCheckPeriod = 60_000;
        /**
         * 设置启动延时为1s（默认延时3s），APP启动1s后初始化SDK，避免影响APP启动速度;
         */
        Beta.initDelay = 1_000;
        /**
         * 设置通知栏大图标，largeIconId为项目中的图片资源;
         */
        Beta.largeIconId = R.mipmap.ic_launcher;
        /**
         * 设置状态栏小图标，smallIconId为项目中的图片资源Id;
         */
        Beta.smallIconId = R.mipmap.ic_launcher;
        /**
         * 设置更新弹窗默认展示的banner，defaultBannerId为项目中的图片资源Id;
         * 当后台配置的banner拉取失败时显示此banner，默认不设置则展示“loading“;
         */
        Beta.defaultBannerId = R.mipmap.ic_launcher;
        /**
         * 设置sd卡的Download为更新资源保存目录;
         * 后续更新资源会保存在此目录，需要在manifest中添加WRITE_EXTERNAL_STORAGE权限;
         */
        Beta.storageDir = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        /**
         * 已经确认过的弹窗在APP下次启动自动检查更新时会再次显示;
         */
        Beta.showInterruptedStrategy = true;
        /**
         * 只允许在MainActivity上显示更新弹窗，其他activity上不显示弹窗; 不设置会默认所有activity都可以显示弹窗;
         */
        Beta.canShowUpgradeActs.add(MainActivity.class);

        /***** Bugly高级设置 *****/
        BuglyStrategy strategy = new BuglyStrategy();
        /**
         * 设置app渠道号
         */
        strategy.setAppChannel(BUGLY_APP_CHANNEL);

        /***** 统一初始化Bugly产品，包含Beta *****/
        Bugly.init(this, BUGLY_APP_ID, true, strategy);
    }

}
